package org.abratuhi.kvngroupware.scenario.widget;

import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.KVNGroupwareClientAction;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.manager.Manager;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.scenario.model.Scenario;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.widget.AbstractUIWidget;
import org.jdom.Element;

@SuppressWarnings("serial")
public class ScenariolistWidget extends AbstractUIWidget{
	
	LoginManager loginmanager;
	
	JButton btnCreate = new JButton();
	JButton btnJoin = new JButton();
	JButton btnDelete = new JButton();

	public ScenariolistWidget(String frametitle, MessageSender sender,
			Properties locale, Manager<Scenario> scenariomanager, LoginManager loginmanager) {
		super(frametitle, sender, locale, scenariomanager);
		this.loginmanager =loginmanager;
		
		datapanel.setBorder(new TitledBorder(locale.getProperty("explorer.node.scenarios")));
		
		//btnCreate = new JButton(locale.getPropertyst("uiwidget.button.create"));
		//btnCreate = new JButton(locale.getProperty("uiwidget.button.join"));
		//btnCreate = new JButton(locale.getProperty("uiwidget.button.delete"));
		
		btnCreate.setAction(new CreateAction(locale.getProperty("uiwidget.button.create"), null, null, null));
		btnJoin.setAction(new JoinAction(locale.getProperty("uiwidget.button.join"), null, null, null));
		btnDelete.setAction(new DeleteAction(locale.getProperty("uiwidget.button.delete"), null, null, null));
		
		buttonpanel.setLayout(new SpringLayout());
		buttonpanel.add(btnCreate);
		buttonpanel.add(btnJoin);
		buttonpanel.add(btnDelete);
		SpringUtilities.makeCompactGrid(buttonpanel, 1, 3, 0, 0, 0, 0);
	}

	@Override
	public void onFrameClose() {
		// TODO Auto-generated method stub
		
	}
	class CreateAction extends KVNGroupwareClientAction{
		public CreateAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			String newScenarioTheme = JOptionPane.showInputDialog("Enter Scenario Theme:");
			
			User user = loginmanager.getUser();
			Team team = loginmanager.getTeam();
			Scenario scenario = new Scenario(user.getId()+new Date().getTime(), team.getId(), newScenarioTheme);

			Message message = sender.createMessage(
					"type,action,userId,teamId,scenarioId", 
					"scenario,create,"+user.getId()+","+team.getId()+","+scenario.getId(),
					new String(),
					new Element[]{scenario.toElement()});
			sender.sendMessage(message);
		}
	}
	
	class JoinAction extends KVNGroupwareClientAction{
		public JoinAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(datalist.getModel().getSize()>0 && datalist.getSelectedIndex()!=-1){
				if(datalist.getSelectedIndex() != -1){
					String selectedScenario = (String) datalist.getSelectedValue();
					String selectedScenarioId = selectedScenario.substring(selectedScenario.indexOf("|")+1);
					
					User user = loginmanager.getUser();
					Team team = loginmanager.getTeam();
					Scenario scenario = (Scenario) manager.get(selectedScenarioId);
					
					Message message = sender.createMessage(
							"type,action,userId,teamId,scenarioId", 
							"scenario,join,"+user.getId()+","+team.getId()+","+scenario.getId(),
							new String(),
							new Element[]{scenario.toElement()});
					sender.sendMessage(message);
				}
			}
		}
	}
	
	class DeleteAction extends KVNGroupwareClientAction{
		public DeleteAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(datalist.getModel().getSize()>0 && datalist.getSelectedIndex()!=-1){
				if(datalist.getSelectedIndex() != -1){
					String selectedScenario = (String) datalist.getSelectedValue();
					String selectedScenarioId = selectedScenario.substring(selectedScenario.indexOf("|")+1);
					
					User user = loginmanager.getUser();
					Team team = loginmanager.getTeam();
					Scenario scenario = (Scenario) manager.get(selectedScenarioId);
					
					Message message = sender.createMessage(
							"type,action,userId,teamId,scenarioId", 
							"scenario,delete,"+user.getId()+","+team.getId()+","+scenario.getId(),
							new String(),
							new Element[]{scenario.toElement()});
					sender.sendMessage(message);
				}
			}
		}
	}

}

